package com.qf.taobei.service.impl;


import com.qf.taobei.entity.*;
import com.qf.taobei.entity.bo.CartAdd;
import com.qf.taobei.entity.vo.OrderDetailsVo;
import com.qf.taobei.entity.OrderProduct;
import com.qf.taobei.entity.vo.ProductVo;
import com.qf.taobei.entity.vo.StoreOrderVo;
import com.qf.taobei.entity.vo.ViewOrderDetails;
import com.qf.taobei.mapper.OrderMapper;
import com.qf.taobei.mapper.ProductMapper;
import com.qf.taobei.service.OrderService;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 吴彦祖
 * @version 1.0
 * @description: TODO
 * @date 2022/2/26 11:34
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ProductMapper productMapper;
    /**
     * 通过订单编号查询某个订单
     * @param orderNumber
     * @return
     */
    @Override
    public List<Order> selectOrderByOrderNumber(Integer uid,String orderNumber) {
        List<Order> orders = orderMapper.selectOrderByOrderNumber(uid,orderNumber);
        return orders;

    }
    /**
     * 管理员通过订单编号查询某个订单
     * @param orderNumber
     * @return
     */
    @Override
    public List<Order> queryOrderByOrderNumber(String orderNumber) {
        List<Order> orders = orderMapper.queryOrderByOrderNumber(orderNumber);
        return orders;

    }
    @Override
    public List<Order> selectOrderListByUserId(Integer userId) {
        List<Order> orders = orderMapper.selectOrderListByUserId(userId);
        return orders;
    }

    @Override
    public List<OrderDetailsVo> selectOrderDetailsByOrderId(Integer orderId) {
        return orderMapper.selectOrderDetailsByOrderId(orderId);
    }
    @Override
    public OrderDetailsVo selectOrderDetailsByOrderNumber(String orderNumber) {
        return orderMapper.selectOrderDetailsByOrderNumber(orderNumber);
    }

    @Override
    public List<OrderDetailsVo> selectAllStoreOrderByUserId(Integer userId) {
        return orderMapper.selectAllStoreOrderByUserId(userId);
    }

    @Override
    public Integer updeteOrderByOrderId(Order order) {
        return orderMapper.updeteOrderByOrderId(order);

    }

    @Override
    public Integer userDelOrderByOrderId(Order order) {
        order.setUserDelete(0);
        return orderMapper.updeteOrderByOrderId(order);
    }

    @Override
    public Integer stroeDelOrderByOrderId(Order order) {
        order.setStoreDelete(0);
        return orderMapper.updeteOrderByOrderId(order);
    }

    @Override
    public Order addOrderByUserId(Integer userId, List<CartAdd> list) {
        Order order = new Order();
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
        order.setOrderNumber(uuid);
        order.setUserDelete(1);
        order.setStoreDelete(1);
        order.setStatus(2);
        order.setUid(userId);
        SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
        order.setCreateTime(ft.format(new Date()));
        System.out.println(order+"-------");

        orderMapper.addOrderByUserId(order);
        OrderDetailsVo orderDetailsVo = orderMapper.selectOrderDetailsByOrderNumber(uuid);
        OrderProduct orderProduct = new OrderProduct();
        orderProduct.setOrderId(orderDetailsVo.getId());
        order.setId(orderDetailsVo.getId());
        List<OrderProduct> orderProductList = new ArrayList<>();
        double sum = 0;
        ProductVo productVo = null;
        for (int i = 0; i < list.size(); i++) {
            productVo = productMapper.queryProductByIdForAfter(list.get(i).getPId());
            sum = (productVo.getPrice())*(list.get(i).getNumber());
            orderProduct.setPid(list.get(i).getPId());
            orderProduct.setNumber(list.get(i).getNumber());
            order.setTotalPrice(sum);
            System.out.println(order);
            orderMapper.updeteOrderByOrderId(order);
            orderProductList.add(orderProduct);
        }
        for (int i = 0; i < orderProductList.size(); i++) {
            orderMapper.addProductInOrder(orderProductList.get(i));
        }
        return order;
    }

//    @Override
//    public Comment addCommentByProductId(Integer pid) {
//        return orderMapper.addCommentByProductId(pid);
//
//    }

    @Override
    public List<StoreOrderVo> selectAllOrderByStoreId(Integer storeId) {
        return orderMapper.selectAllOrderByStoreId(storeId);
    }

    @Override
    public Integer delOrder(String orderNumber) {
        return orderMapper.delOrder(orderNumber);
    }

    @Override
    public List<ViewOrderDetails> queryOrderDetailsByOrderId(Integer id) {
        return orderMapper.queryOrderDetailsByOrderId(id);
    }
    @Override
    public RespBean getOrderList() {
        List<Order> orders = orderMapper.getOrderList();
        return RespBean.respSuccess("成功",orders);
    }

    @Override
    public List<StoreOrderVo> selectOrderNumberLikeByStore(Integer storeId,String orderNumber) {
        return orderMapper.selectOrderNumberLikeByStore(storeId,orderNumber);
    }

    @Override
    public List<Order> getOrderListByStatus(Integer id, Integer status) {
        return orderMapper.getOrderListByStatus(id,status);
    }

    @Override
    public Integer deleteOrderByOrderId(Integer id) {
        return orderMapper.deleteOrderByOrderId(id);
    }

}
